package com.plan

import grails.converters.JSON

class WorkplanDetailController {

    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[workplanDetailInstanceList: WorkplanDetail.list(params), workplanDetailInstanceTotal: WorkplanDetail.count()]
	}

    def create = {
        def workplanDetailInstance = new WorkplanDetail()
        def workplan = Workplan.get(params?.parentId)
        workplanDetailInstance.parent = workplan
        workplanDetailInstance.properties = params
        return [workplanDetailInstance: workplanDetailInstance,parentId:params?.parentId]
    }

    def save = {
        def workplanDetailInstance = new WorkplanDetail(params)
        if (workplanDetailInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), workplanDetailInstance.id])
            redirect(action: "edit", id: workplanDetailInstance.id)
        }
        else {
            render(view: "create", model: [workplanDetailInstance: workplanDetailInstance])
        }
    }

    def show = {
        def workplanDetailInstance = WorkplanDetail.get(params.id)
       
        if (!workplanDetailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
            redirect(action: "list")
        }
        else {
        		 params.parentId = workplanDetailInstance.parent.Id
            [workplanDetailInstance: workplanDetailInstance]
        }
    }

    def edit = {
        def workplanDetailInstance = WorkplanDetail.get(params.id)
        if (!workplanDetailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
            redirect(action: "list")
        }
        else {
        		params.parentId = workplanDetailInstance.parent.id
            return [workplanDetailInstance: workplanDetailInstance]
        }
    }

    def update = {
        def workplanDetailInstance = WorkplanDetail.get(params.id)
        if (workplanDetailInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (workplanDetailInstance.version > version) {
                    
                    workplanDetailInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'workplanDetail.label', default: 'WorkplanDetail')] as Object[], "Another user has updated this WorkplanDetail while you were editing")
                    render(view: "edit", model: [workplanDetailInstance: workplanDetailInstance])
                    return
                }
            }
            workplanDetailInstance.properties = params
            if (!workplanDetailInstance.hasErrors() && workplanDetailInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), workplanDetailInstance.id])
                redirect(action: "edit", id: workplanDetailInstance.id)
            }
            else {
                render(view: "edit", model: [workplanDetailInstance: workplanDetailInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def workplanDetailInstance = WorkplanDetail.get(params.id)
        def parentId = workplanDetailInstance.parent.id
        if (workplanDetailInstance) {
            try {
                workplanDetailInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
                redirect(controller:"workplan",action: "edit",id:parentId)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'workplanDetail.label', default: 'WorkplanDetail'), params.id])
           redirect(controller:"workplan",action: "edit",id:parentId)
        }
    }
}
